﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NOVA.AderTemplates;
using Microsoft.SharePoint;
using System.Web;

namespace AgileShare.Framework.Template.TemplateEngine
{
    public enum TemplateSourceType
    {
        String,
        File
    }

    public class TemplateEngine
    {
        public DefaultEngine defaultEng;
        /// <summary>
        /// 初始化模板引擎
        /// </summary>
        /// <param name="sharepointstylepath">sharepoint源路径</param>
        public TemplateEngine(string sharepointstylepath) {
            string strSource = GetTemplateHtml(sharepointstylepath);
            defaultEng = new DefaultEngine(strSource);
        }
        /// <summary>
        /// 初始化模板引擎
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="source">源</param>
        public TemplateEngine(TemplateSourceType type,string source) {
            switch (type)
            {
                case TemplateSourceType.String:
                    defaultEng = new DefaultEngine(source);
                    break;
                case TemplateSourceType.File:
                    defaultEng = new DefaultEngine(source, Encoding.Default);
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 输出解析结果
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string ProcessTemplate(Dictionary<string,object> data ){

            string result = string.Empty;
            if (data.Count>0)
            {
                foreach (KeyValuePair<string,object> item in data)
                {
                    defaultEng.TemplateManager.SetValue(item.Key, item.Value);
                }
                result = defaultEng.TemplateManager.Process();
            }
            return result;
        }
        /// <summary>
        /// 加载模板
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string GetTemplateHtml(string path) {
            string url = MakeServerRelativeUrl(path);
            string html = SPContext.GetContext(HttpContext.Current).Web.Site.RootWeb.GetFileAsString(url);
            return html;
        }
        /// <summary>
        /// 获取模板服务器路径
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string MakeServerRelativeUrl(string url)
        {
            return concatenateUrls(SPContext.GetContext(HttpContext.Current).Site.ServerRelativeUrl, url);
        }
        /// <summary>
        /// 合并服务器路径字符串
        /// </summary>
        /// <param name="urlA"></param>
        /// <param name="urlB"></param>
        /// <returns></returns>
        private string concatenateUrls(string urlA, string urlB)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(urlA);
            if (string.Compare("/", urlB, StringComparison.OrdinalIgnoreCase) != 0)
            {
                if (urlA.EndsWith("/", StringComparison.OrdinalIgnoreCase))
                {
                    if (urlB.StartsWith("/", StringComparison.OrdinalIgnoreCase))
                    {
                        builder.Append(urlB.Substring(1));
                    }
                    else
                    {
                        builder.Append(urlB);
                    }
                }
                else if (urlB.StartsWith("/", StringComparison.OrdinalIgnoreCase))
                {
                    builder.Append(urlB);
                }
                else
                {
                    builder.Append("/");
                    builder.Append(urlB);
                }
            }
            return builder.ToString();
        }

    }
}
